import BaseConfig, { IItemConfig } from "../../../Script/Base/BaseConfig";
import BaseSprite from "../../../Script/Base/BaseSprite";
import { GlobalData } from "../../../Script/Common/GlobalData";
import { IBagItemData } from "../../../Script/Interface/IMessage";
import StorageUtil from "../../Util/StorageUtil";

const { ccclass, property } = cc._decorator;

@ccclass
export default class CollectItem extends BaseSprite {

    @property({ type: cc.Label, tooltip: "物品名字" })
    private labName: cc.Label = null;
    @property({ type: cc.Label, tooltip: "拥有数量" })
    private labHaveNum: cc.Label = null;
    @property({ type: cc.Label, tooltip: "需要数量" })
    private labNeedNum: cc.Label = null;
    @property({ type: cc.Label, tooltip: "市场价格" })
    private labBase: cc.Label = null;
    @property({ type: cc.Label, tooltip: "市场价箭头" })
    private labArrow: cc.Label = null;
    @property({ type: cc.Label, tooltip: "市场比例" })
    private labRatio: cc.Label = null;
    @property({ type: cc.Label, tooltip: "收购价" })
    private labAdd: cc.Label = null;

    private data: { id: number, num: number } = null;
    private config: IItemConfig = null;
    private bagData: IBagItemData = null;

    public setData(data: { id: number, num: number }) {
        this.data = data;
        this.bagData = GlobalData.getBagDataById(data.id);
        this.config = BaseConfig.instance.getItemInfoById(data.id);

        this.refresh();
    }

    public refresh() {
        const realPrice = this.getRealPrice();
        this.labName.string = `${this.config.name}`;
        this.labHaveNum.string = `${this.bagData?.num || 0}`;
        this.labNeedNum.string = `/${this.data.num}`;
        this.labBase.string = `${this.config.basePrice}`;
        this.labRatio.string = `${realPrice.ratio}%`;
        this.labAdd.string = `${realPrice.price}`;

        if (this.bagData?.num >= this.data.num) {
            this.labHaveNum.node.active = false;
            this.labNeedNum.string = `${this.bagData?.num}/${this.data.num}`;
        } else {
            this.labHaveNum.node.active = true;
            this.labNeedNum.string = `/${this.data.num}`;
        }
    }

    public getRealPrice() {
        const gameData = StorageUtil.instance.getGameData();
        this.labArrow.node.active = gameData.addPriceCount > 0;
        this.labRatio.node.active = gameData.addPriceCount > 0;

        const config = GlobalData.getCollectConfigById(gameData.addPriceCount);
        if (config) {
            const price = Math.floor(this.config.basePrice * config.ratio / 100);
            return { price: price, ratio: config.ratio };
        } else {
            console.log("配置不存在~");
        }

        // let base = this.config.basePrice * this.data.num;
        // let shoper = this.config.basePrice * this.data.num;
        // base = base * Math.pow(1.5, gameData.addPriceCount);
        // shoper = shoper * Math.pow(1.5, gameData.addPriceCount);
        // return { base: Math.floor(base), shoper: Math.floor(shoper) };
    }

    public getComplete() {
        return this.bagData?.num >= this.data.num;
    }

    private onPlaceClicked(event, eventData) {
        console.log("掉落地点:", this.config.drop);
    }
}